﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using KpBusiness.Layer;
using NHibernate;
using KpBusiness.Model;
using NHibernate.Criterion;

namespace KpBusiness.Service
{
    public class UserInformationService
    {
        private static readonly ILog log = LogManager.GetLogger("Services");
       
        /// <summary>
        /// Return all Registered users
        /// </summary>
        /// <returns>Returens all users</returns>
        public IList<UserInformation> GetAllUsersInformation()
        {

            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            IList<UserInformation> students = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();


                ICriteria criteria = session.CreateCriteria(typeof(UserInformation))
                    .SetFetchMode("Role", FetchMode.Join);
                    
                students = criteria.List<UserInformation>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return students;
        }

        /// <summary>
        /// Gets UserInformation
        /// </summary>
        /// <param name="userInformationId">Provide User's Id</param>
        /// <returns>Returns UserInformation</returns>
        public UserInformation GetUserInformation(int userInformationId)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            UserInformation userInformation = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                userInformation = session.Get<UserInformation>(userInformationId);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return userInformation;
        }

        /// <summary>
        /// Registers User into System
        /// </summary>
        /// <param name="userInformation">Provide UserInformation</param>
        /// <returns>returns identity</returns>
        public int CreateUserInformation(UserInformation userInformation)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenWriterSession();
            int userId = 0;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                userId = (int)session.Save(userInformation);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                    
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return userId;
        }

        /// <summary>
        /// Updates Personal Information
        /// </summary>
        /// <param name="userInformation">Provide User information</param>
        public void UpdateUserInformation(UserInformation userInformation)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenWriterSession();
            
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                session.SaveOrUpdate(userInformation);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();

                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            
        }


        /// <summary>
        /// Gets UserInformation
        /// </summary>
        /// <param name="userInformationId">Provide User's Name</param>
        /// <returns>Returns UserInformation</returns>
        public UserInformation GetUserInformationByUserName(string userName)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            UserInformation userInformation = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                ICriteria criteria = session.CreateCriteria(typeof(UserInformation))
                  .Add(Restrictions.Eq("UserName", userName));

                userInformation = criteria.UniqueResult<UserInformation>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return userInformation;
        }


        /// <summary>
        /// Gets UserInformation
        /// </summary>
        /// <param name="userInformationId">Provide User's Name</param>
        /// <returns>Returns UserInformation</returns>
        public UserInformation ValidateUserInformation(string userName,string password)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            UserInformation userInformation = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                ICriteria criteria = session.CreateCriteria(typeof(UserInformation))
                  .Add(Restrictions.Eq("UserName", userName))
                  .Add(Restrictions.Eq("Password", password));

                userInformation = criteria.UniqueResult<UserInformation>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return userInformation;
        }
    }
}
